/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.event;

import java.io.Serializable;

import x10.enums.Device;
import x10.enums.Function;
import x10.enums.House;
import x10.enums.Media;

/**
 *
 * @author Denny
 */
public class X10Command extends X10Event implements Serializable {
    static final long serialVersionUID = 1L;
    private String address;
    private String source;
    private long instantiatedMillis;

    private House houseCode;
    private Device deviceCode;
    private Function functionCode;
    private int dimBrightLevel;
    private Media media;

    public X10Command(House houseCode, Device deviceCode, Function functionCode, int dimBrightLevel, Media media, String source){
        this.houseCode = houseCode;
        this.deviceCode = deviceCode;
        this.functionCode = functionCode;
        this.dimBrightLevel = dimBrightLevel;
        this.media = media;
        this.source = source;
        this.address = houseCode.toString() + deviceCode.toString();
        this.instantiatedMillis = System.currentTimeMillis();
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public Function getFunctionCode() {
        return functionCode;
    }

    public void setFunctionCode(Function functionCode) {
        this.functionCode = functionCode;
    }

    public House getHouseCode() {
        return houseCode;
    }

    public void setHouseCode(House houseCode) {
        this.houseCode = houseCode;
    }

    public Device getDeviceCode() {
        return deviceCode;
    }

    public void setDeviceCode(Device deviceCode) {
        this.deviceCode = deviceCode;
    }

    public int getDimBrightLevel() {
        return dimBrightLevel;
    }

    public void setDimBrightLevel(int dimBrightLevel) {
        this.dimBrightLevel = dimBrightLevel;
    }

    public Media getMedia() {
        return media;
    }

    public void setMedia(Media media) {
        this.media = media;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("X10 Command ");
        sb.append("Address:");
        sb.append(getAddress());
        sb.append(" ");
        sb.append("Function:");
        sb.append(getFunctionCode());
        sb.append(" ");
        sb.append("Dim:");
        sb.append(getDimBrightLevel());
        sb.append(" ");
        sb.append("Media:");
        sb.append(getMedia().toString());
        sb.append(" ");
        sb.append("Source:");
        sb.append(getSource());

        return sb.toString();
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10Command){
            X10Command other = (X10Command)device;
            if(other!=null){
                if(!this.getAddress().contentEquals(other.getAddress())){
                    isEqual = false;
                }
                if(!this.getSource().contentEquals(other.getSource())){
                    isEqual = false;
                }
                if(this.houseCode != other.getHouseCode()){
                    isEqual = false;
                }
                if(this.deviceCode != other.getDeviceCode()){
                    isEqual = false;
                }
                if(this.functionCode != other.getFunctionCode()){
                    isEqual = false;
                }
                if(this.media != other.getMedia()){
                    isEqual = false;
                }
                if(this.dimBrightLevel != other.getDimBrightLevel()){
                    isEqual = false;
                }

                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }
}
